﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace IllustratedCsharp7_ch21
{
    delegate long MyDel(int first, int second); //声明委托类型
    class WaitUntilDown
    {
        static long Sum(int x, int y)           //声明异步方法
        {
            Console.WriteLine("             Inside Sum");
            Thread.Sleep(5000);
            return x + y;
        }

        static void _Main()
        {
            MyDel del = new MyDel(Sum);
            Console.WriteLine("Before BeginInvoke");

            IAsyncResult iar = del.BeginInvoke(3, 5, null, null);   //开始异步调用
            //Do additional work in the calling thread,while the method
            // is being executed asynchronously in the spawned thread.
            //在调用线程中做额外的工作，同时在产生的线程中异步执行该方法。
            Console.WriteLine("After BeginInvoke");
            Console.WriteLine("Donig stuff");
            long result = del.EndInvoke(iar);//等待结束并获取结果

            Console.WriteLine("After EndInvoke:{0}", result);

            Console.Read();
        }
    }
}
